package com.gun.management.module.task.controller.admin.taskInfo;

import com.gun.management.framework.common.pojo.CommonResult;
import com.gun.management.framework.common.pojo.PageResult;
import com.gun.management.framework.common.util.object.BeanUtils;
import com.gun.management.module.task.controller.admin.taskInfo.vo.TaskInfoEnabledReqVO;
import com.gun.management.module.task.controller.admin.taskInfo.vo.TaskInfoPageReqVO;
import com.gun.management.module.task.controller.admin.taskInfo.vo.TaskInfoRespVO;
import com.gun.management.module.task.controller.admin.taskInfo.vo.TaskInfoSaveReqVO;
import com.gun.management.module.task.service.taskInfo.TaskInfoService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import jakarta.annotation.Resource;

import static com.gun.management.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 任务信息")
@RestController
@RequestMapping("/task/taskInfo")
@Validated
public class TaskInfoController {

    @Resource
    private TaskInfoService taskInfoService;

    @PostMapping("/create")
    @Operation(summary = "创建任务信息")
    @PreAuthorize("@ss.hasPermission('task:taskInfo:create')")
    public CommonResult<Long> createInfo(@Valid @RequestBody TaskInfoSaveReqVO createReqVO) {
        return success(taskInfoService.createInfo(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新任务信息")
    @PreAuthorize("@ss.hasPermission('task:taskInfo:update')")
    public CommonResult<Boolean> updateInfo(@Valid @RequestBody TaskInfoSaveReqVO updateReqVO) {
        taskInfoService.updateInfo(updateReqVO);
        return success(true);
    }


    @DeleteMapping("/delete")
    @Operation(summary = "删除任务信息")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('task:taskInfo:delete')")
    public CommonResult<Boolean> deleteInfo(@RequestParam("id") Long id) {
        taskInfoService.deleteInfo(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得任务信息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('task:taskInfo:query')")
    public CommonResult<TaskInfoRespVO> getInfo(@RequestParam("id") Long id) {
        return success(taskInfoService.getTaskDetailById(id));
    }

    @GetMapping("/page")
    @Operation(summary = "获得任务信息分页")
    @PreAuthorize("@ss.hasPermission('task:taskInfo:page')")
    public CommonResult<PageResult<TaskInfoRespVO>> getInfoPage(@Valid TaskInfoPageReqVO pageReqVO) {
        PageResult<TaskInfoRespVO> pageResult = taskInfoService.getInfoPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, TaskInfoRespVO.class));
    }

//    @GetMapping("/export-excel")
//    @Operation(summary = "导出任务信息 Excel")
//    @PreAuthorize("@ss.hasPermission('task:taskInfo:export')")
//    @ApiAccessLog(operateType = EXPORT)
//    public void exportInfoExcel(@Valid TaskInfoPageReqVO pageReqVO,
//              HttpServletResponse response) throws IOException {
//        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
//        List<TaskInfoDO> list = taskInfoService.getInfoPage(pageReqVO).getList();
//        // 导出 Excel
//        ExcelUtils.write(response, "任务信息.xls", "数据", TaskInfoRespVO.class,
//                        BeanUtils.toBean(list, TaskInfoRespVO.class));
//    }


    @PutMapping("/updateStatusEnabled")
    @Operation(summary = "修改状态")
    @PreAuthorize("@ss.hasPermission('task:taskInfo:updateStatusEnabled')")
    public CommonResult<Boolean> updateStatusEnabled(@Valid @RequestBody TaskInfoEnabledReqVO updateReqVO) {
        taskInfoService.updateStatusEnabled(updateReqVO.getId(), updateReqVO.getCategoryId(),updateReqVO.getEnabled());
        return success(true);
    }

}